package com.pruebas.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.Type;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findUsersByUsernameEquals" })
@Table(name = "USERS")
public class User {

    @NotNull
    private String password;

    @NotNull
    @Column(unique = true)
    private String username;

    @NotNull
    @Column(unique = true)
    private String email;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<Authority> rolesList = new HashSet<Authority>();

    @ManyToMany(cascade = CascadeType.ALL)
    private List<ExamModel> examModelList = new ArrayList<ExamModel>();

    @NotNull
    @Type(type = "yes_no")
    private Boolean enabled;

    public static com.pruebas.entity.User userByUserNameAndPassword(String username, String password) {
        if (username == null || username.length() == 0) throw new IllegalArgumentException("The username argument is required");
        EntityManager em = new User().entityManager;
        TypedQuery<User> q = em.createQuery("SELECT o FROM User AS o WHERE o.username = :username and o.password = :password", User.class);
        q.setParameter("username", username);
        q.setParameter("password", password);
        return q.getSingleResult();
    }

    public List<com.pruebas.entity.ExamModel> getExamModelList() {
        if (examModelList == null) {
            examModelList = new ArrayList<ExamModel>();
        }
        return examModelList;
    }

    public void setExamModelList(List<com.pruebas.entity.ExamModel> examModelList) {
        this.examModelList = examModelList;
    }
}
